package com.bringway.p2p.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.p2p.dao.VersionDao;

/**
 * 
 * @author qjy
 * @version 创建时间：2016年06月14日 上午13:25:42
 * @功能：查询异常信息
 */

@Repository
public class VersionDaoImpl  extends P2PJdbcDao implements VersionDao{
	Logger logger = Logger.getLogger(VersionDaoImpl.class);
	
	
	public List<Map<String, Object>> queryVersionList(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		if(StringUtils.isNotEmpty(startDate)){
			startDate=startDate+" 00:00:00";
		}
		if(StringUtils.isNotEmpty(endDate)){
			endDate=endDate+" 23:59:59";
		}
		String version=params.optString("version");
		String versionname=params.optString("versionname");
		String sblx=params.optString("sblx");
		String sjlx=params.optString("sjlx");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("start", start);
		paramMap.put("limit", limit);
		sql.append(" select ID, CUR_VERSION, CUR_VERSION_NAME, DEVICE_TYPE, DOWNLOAD_PATH,"
				+ " VERSION_COMMENT, UPDATE_FLAG ,date_format(CREATE_DATE,'%Y-%m-%d %H:%i:%s') CREATE_DATE "
				+ " from t_zhfw_app_version_info ");
		sql.append(" where 1=1  ");
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND date_format(LOG_DATE,'%Y-%m-%d %H:%i:%s') >= :startDate ");
			paramMap.put("startDate",startDate);
		}
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND date_format(LOG_DATE,'%Y-%m-%d %H:%i:%s') <= :endDate ");
			paramMap.put("endDate",endDate);
		}
		
		if(StringUtils.isNotEmpty(version)){
			sql.append(" AND CUR_VERSION like "+ "'%"+version+"%'");
		}
		if(StringUtils.isNotEmpty(versionname)){
			sql.append(" AND CUR_VERSION_NAME like "+ "'%"+versionname+"%'");
		}
		if(StringUtils.isNotEmpty(sblx)){
			sql.append(" AND DEVICE_TYPE =:sblx");
			paramMap.put("sblx", sblx);
		}
		if(StringUtils.isNotEmpty(sjlx)){
			sql.append(" AND UPDATE_FLAG =:sjlx");
			paramMap.put("sjlx", sjlx);
		}
		if(0 != limit){
			sql.append(" ORDER BY CREATE_DATE DESC LIMIT :start,:limit  ");
		}
		logger.info("queryVersionList sql is " + sql.toString());
		logger.info("queryVersionList paramMap is " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramMap);
	}

	@Override
	public int getVersionCount(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		String version=params.optString("version");
		String versionname=params.optString("versionname");
		String sblx=params.optString("sblx");
		String sjlx=params.optString("sjlx");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("start", start);
		paramMap.put("limit", limit);
		sql.append(" SELECT count(*) from t_zhfw_app_version_info ");
		sql.append("   where 1=1 ");
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND date_format(LOG_DATE,'%Y-%m-%d %H:%i:%s') >= :startDate ");
			paramMap.put("startDate",startDate);
		}
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND date_format(LOG_DATE,'%Y-%m-%d %H:%i:%s') <= :endDate ");
			paramMap.put("endDate",endDate);
		}
		
		if(StringUtils.isNotEmpty(version)){
			sql.append(" AND CUR_VERSION like "+ "'%"+version+"%'");
		}
		if(StringUtils.isNotEmpty(versionname)){
			sql.append(" AND CUR_VERSION_NAME like "+ "'%"+versionname+"%'");
		}
		if(StringUtils.isNotEmpty(sblx)){
			sql.append(" AND DEVICE_TYPE =:sblx");
			paramMap.put("sblx", sblx);
		}
		if(StringUtils.isNotEmpty(sjlx)){
			sql.append(" AND UPDATE_FLAG =:sjlx");
			paramMap.put("sjlx", sjlx);
		}
		logger.info("getVersionCount sql is " + sql.toString());
		logger.info("getVersionCount paramMap is " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}

	@Override
	public int updateVersion(JSONObject params) {
		StringBuffer sql = new StringBuffer();
		String id = params.optString("id");
		String versionname = params.optString("versionname");
		String dload = params.optString("dload");
		String content = params.optString("content");
		String sjlx = params.optString("sjlx");
		String sblx = params.optString("sblx");
		sql.append(" UPDATE t_zhfw_app_version_info SET CUR_VERSION_NAME=:CUR_VERSION_NAME,DEVICE_TYPE=:DEVICE_TYPE,"
				+ " DOWNLOAD_PATH=:DOWNLOAD_PATH,VERSION_COMMENT=:VERSION_COMMENT,UPDATE_FLAG=:UPDATE_FLAG,CREATE_DATE=now() "
				+ " WHERE ID=:ID ");
		Map<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("ID", id);
		paramsMap.put("CUR_VERSION_NAME", versionname);
		paramsMap.put("DOWNLOAD_PATH", dload);
		paramsMap.put("VERSION_COMMENT", content);
		paramsMap.put("UPDATE_FLAG", sjlx);
		paramsMap.put("DEVICE_TYPE", sblx);
		logger.info("updateVersion sql is " + sql.toString());
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), paramsMap);
	}
	@Override
	public int addVersion(JSONObject params) {
		String version = params.optString("version");
		String versionname = params.optString("versionname");
		String sblx = params.optString("sblx");
        String dload = params.optString("dload");
        String content = params.optString("content");
        String sjlx = params.optString("sjlx");
		StringBuffer sql  =  new StringBuffer();
		sql.append(" INSERT INTO t_zhfw_app_version_info (ID, CUR_VERSION, CUR_VERSION_NAME, DEVICE_TYPE, DOWNLOAD_PATH, VERSION_COMMENT, UPDATE_FLAG, CREATE_DATE) VALUES "+
		" (uuid(), :CUR_VERSION, :CUR_VERSION_NAME, :DEVICE_TYPE, :DOWNLOAD_PATH, :VERSION_COMMENT, :UPDATE_FLAG, now()) ");
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("CUR_VERSION", version);
		paramsMap.put("CUR_VERSION_NAME", versionname);
		paramsMap.put("DEVICE_TYPE", sblx);
		paramsMap.put("DOWNLOAD_PATH", dload);
		paramsMap.put("VERSION_COMMENT", content);
		paramsMap.put("UPDATE_FLAG", sjlx);
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), paramsMap);
	}
}
